<!doctype html>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<script>

window.jsTestIsAsync = true;
description('Tests that XHR respects "Cache-Control" header.');

function makeXHR(url, headers)
{
    return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        for (var i = 0; i < headers.length; ++i) {
            xhr.setRequestHeader(headers[i].name, headers[i].value);
        }
        xhr.onreadystatechange = function() {
            if (xhr.readyState === xhr.DONE) {
                if (xhr.status === 200) {
                    resolve(xhr.responseText);
                } else {
                    reject(xhr.status);
                }
            }
        }
        xhr.onerror = reject;
        xhr.send();
    });
}

var withCacheControl = 'resources/echo-random.php';
var cacheControl = [{name: 'Cache-Control', value: 'no-cache'}];
Promise.all([makeXHR(withCacheControl, cacheControl), makeXHR(withCacheControl, cacheControl)]).then(function(res1, res2) {
    if (res1 === res2) {
        // This is a bit flaky, because two different requests can accidentaly
        // result in the same response.
        testFailed("MemoryCache seems to be used.");
    } else {
        testPassed("MemoryCache is not used.");
    }
    finishJSTest();
}, function() {
    testFailed('rejected');
    finishJSTest();
});
</script>
</body>
</html>
